<template>
  <div class='com-container' @dblclick="revertChina">
    <div class='com-chart' ref='map_ref'></div>
  </div>
</template>

<script>
import '../../../public/static/theme/chalk.js'
import axios from "axios";
import {getProvinceMapInfo} from "@/utils/koa2/utils/map_utils";

export default {
  name: "Map2",
  data() {
    return {
      chartInstance: null,
      data: null,
      mapData: {}
    }
  },
  mounted() {
    this.initChartInstance()
    this.getData()
    window.addEventListener("resize", this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    async initChartInstance() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, 'chalk')
      const {data: res} = await axios.get('http://localhost:9528/static/map/china.json')
      this.$echarts.registerMap('china', res)
      const initOption = {
        title: {
          text: '🗺 分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72BF',
            borderColor: '#333',

          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async (arg) => {
        const provinceInfo = getProvinceMapInfo(arg.name)
        // 需要获取这个省份的矢量数据
        // 判断当前所点击的省份的地图矢量数据在mapData中是否存在
        if (!this.mapData[provinceInfo.key]) {
          const {data: res} = await axios.get('http://localhost:9528' + provinceInfo.path)
          this.mapData[provinceInfo.key] = res
          this.$echarts.registerMap(provinceInfo.key, res)
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        }
        this.chartInstance.setOption(changeOption)
      })
    },
    async getData() {
      // 获取服务器的数据，对this.data 进行赋值之后，调用updateChart方法更新图表
      // 散点数据
      const {data: res} = await this.$http.get('map')
      // console.log(res);
      this.data = res
      this.updateChart()
    },
    updateChart() {
      // 处理图表需要的数据
      const seriesArr = this.data.map(item => {
        // return的这个对象就代表的是一个类别下的所有散点数据
        // 如果想在地图中显示散点数据，需要给散点的图标增加一个配置
        return {
          type: 'effectScatter',
          rippleEffect: {
            scale: 7,
            brushType: 'stroke'
          },
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo'
        }
      })
      const legendArr = this.data.map(item => item.name)
      const dataOption = {
        series: seriesArr,
        legend: {
          data: legendArr,

        }
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      // 自适应分辨率
      const titleFontSize = this.$refs.map_ref.offsetWidth / 100 * 2
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()

    },
    // 双击回到中国地图
    revertChina() {
      const revertOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(revertOption)
    }
  }
}
</script>

<style scoped>
.com-chart {
  height: 800px;
  width: 100%;
}
</style>
